VoiceGui{


	classvar <>activeWindow;
	var w, window, <parent, desc, windowCols, colIndex = 0, windowW = 500, windowH = 715, controlsMap;
	
	*new{
		^super.new.init;
	}
	
	init{
		 
		controlsMap = Dictionary.new;
	}
	
	isOpen{
		if(w == nil){
			^false;	
		};
		^w.isClosed.not;
	}
	
	*refesh{
	
	}
	
	setParent{|parentArg|
		parent = parentArg;
	}
	
	setControlValue{|name, value, subControlArg |
	
		var target;
		
		if(w != nil){
			if(w.isClosed){^nil};
		};
	
		if(subControlArg != nil){
			name = name.asString ++ subControlArg.asString;
			name = name.asSymbol;
		};
		
		target = controlsMap.at(name);
		
		if(target != nil){
			{target.setValue(value)}.defer;	
		}
	}
	
	
	currentCol{
		^windowCols.at(colIndex);
	}

	drawWindow{|synthType|
		var bounds, rippleMainBounds;
		
		rippleMainBounds = Ripple.getMainWindowBounds;
		
		this.class.postln;
		rippleMainBounds.postln;
		
		colIndex = 0;	
		
		desc = parent.getDescriptor;
		
		bounds = Rect(rippleMainBounds.left + 15, rippleMainBounds.top, windowW, windowH );
		
		w = SCWindow.new(parent.getName  + " : " + synthType, bounds);
		
		
		if(VoiceGui.activeWindow.notNil){
			if(VoiceGui.activeWindow.isClosed.not){
				VoiceGui.activeWindow.close
			}
		};
		VoiceGui.activeWindow = w;
		
		this.setKeyDownActions;
		
		window = FlowView.new(w, Rect(0, 0, 800, windowH));
		window.background_(MPGuiConstants.getBackground);
		window.decorator.gap_(0@0);
		window.decorator.margin_(10@10);
		window.decorator.nextLine;
		
		windowCols = Array.new(2);
		
		windowCols.add(FlowView.new(window, 250@windowH) );
		windowCols.add(FlowView.new(window, 250@windowH) );
		
		this.buildVoiceControls;
		
		this.verticalSpace(20);
		
		this.buildVoiceUtils;
		
		w.front;
		
	}
	
	verticalSpace{|amount|
		this.currentCol.decorator.nextLine;
		/* spacer */ SCStaticText(this.currentCol, 5 @ amount);
		this.currentCol.decorator.nextLine;
	}
	
	nextLine{|col|
		col.decorator.nextLine;
	}
		
	buildVoiceControls{
		var l, i=0;	
		desc.keysValuesDo{|name, control|
			var widget;
			i = i + 1;
			if(i > 15){ colIndex = 1;};
			
			widget = control.getGuiClass.new(this.currentCol, 380@43, name);
			widget.createActions(parent);
			widget.createControlMapEntries(controlsMap);
			
		}	
	
	}
	
	addToControlsMap{|name, obj|
		// second value in controls map is the function to use to set the value of the object
		controlsMap.put(name, ControlValueSetter.new(obj));
	}
	

	
	buildVoiceUtils{
		var v, b, subCol, armDeleteButton;
		
		// set column index so utils are always in second column
		colIndex = 1;
		
		
		this.verticalSpace(10);
	
		//PLAY / PAUSE
	
		v = SCButton(this.currentCol,120@20);
		v.states = [
			["play",Color.black,Color.yellow],
			["pause",Color.black,Color.white]
		];
		v.action = {|butt|
			butt.value.postln;
			if(butt.value == 1)
				{parent.play}
				{parent.pause}
		};
		v.canFocus = false;
		
		
		controlsMap.put(\play, ToggleValueSetter.new(v));
		
	/*
		//spawn
	
		b = SCButton(this.currentCol,50@20);
		b.states = [
			["spawn",Color.black,Color.yellow]
		];
		b.action = {|butt|
			parent.playSoundSustainable
		};
		
			
		//kill
	
		b = SCButton(this.currentCol,50@20);
		b.states = [
			["kill",Color.black,Color.yellow]
		];
		b.action = {|butt|
			parent.releaseSound
		};
		
		*/
		
		
		
		this.verticalSpace(10);
		

		
		//mono / poly
	
//		v = SCButton(this.currentCol,120@20);
//		v.states = [
//			["poly",Color.grey,Color.white],
//			["mono",Color.grey,Color.white]
//		];
//		v.action = {|butt|
//			butt.value.postln;
//			if(butt.value == 0)
//				{parent.setPoly(true)}
//				{parent.setPoly(false)}
//		};
//		v.canFocus = false;
//		
//		controlsMap.put(\poly, ToggleValueSetter.new(v));
//		
//		
//		//this.currentCol.decorator.nextLine;
//		this.verticalSpace(10);
		
		//midi chan 		
		subCol = FlowView.new(this.currentCol, 110@48);
	
		SCStaticText(subCol,120@20).string = 'Midi Channel:';
		
		this.nextLine(subCol);
							
		v = 	SCNumberBox(subCol, 40 @ 20 );
		v.action = {arg numb; parent.setMidiChannel(numb.value);};

		controlsMap.put(\midiChan, NumberBoxSetter.new(v));
		
		
//		//transpose
//		
		subCol = FlowView.new(this.currentCol, 110@48);
//	
		SCStaticText(subCol,120@20).string = 'Transpose:';
//		
		this.nextLine(subCol);
//							
		v = 	SCNumberBox(subCol, 40 @ 20 );
		v.action = {arg numb; parent.setTranspose(numb.value);};
//
		controlsMap.put(\transpose, NumberBoxSetter.new(v));
		
		
		//this.currentCol.decorator.nextLine;
		this.verticalSpace(10);
		
		
		// arm delete
		subCol = FlowView.new(this.currentCol, 80@48);
	
		SCStaticText(subCol,120@20).string = 'Arm Delete';
		
		this.nextLine(subCol);
		
		armDeleteButton = SCButton(subCol,20@20);
		armDeleteButton.states = [
			["",Color.grey,Color.white],
			["",Color.grey,Color.red]
		];
		armDeleteButton.canFocus = false;
		
		
		// delete
		subCol = FlowView.new(this.currentCol, 80@48);
	
		SCStaticText(subCol,120@20).string = 'Delete this Voice';
		
		this.nextLine(subCol);
		
		v = SCButton(subCol,20@20);
		v.states = [
			["!",Color.black,Color.yellow]
		];
		
		v.action = {|butt|
			butt.value.postln;
			if(armDeleteButton.value == 1)
				{
					parent.remove;
					w.close;
				}
				{"#### YOU MUST ARM THE DELETE BEFORE DELETE ####".postln}
		};
		v.canFocus = false;
		
		
		//this.currentCol.decorator.nextLine;
		this.verticalSpace(10);
		
		
		// bus number
		SCStaticText(this.currentCol,120@20).string = 'Main Output Bus';
		
		this.nextLine(this.currentCol);
		
			SCPopUpMenu(this.currentCol, Rect(10, 218, 50, 16))
			.items_(["0-1","2-3","4-5", "6-7", "8-9", "10-11", "12-13", "14-15", "16-17", "18-19"])
			.value_(parent.getMainOutBus / 2)
			.background_(Color.white)
			.action_({ arg ch;
				parent.setMainOutBus(ch.value * 2);
			})
			.canFocus_(false);
		
	
	}
	
	
	
	setKeyDownActions{
		
		w.view.keyDownAction_({|view, char|
			char.postln;
		
			switch(char,
				" "[0], {parent.togglePlayPause;},
				"q"[0], {parent.setControlValue(\volume, "inc");},
				"a"[0], {parent.setControlValue(\volume, "dec");},
				"w"[0], {parent.setControlValue(\volume2, "inc");},
				"s"[0], {parent.setControlValue(\volume2, "dec");}/*",
				
				e"[0], {parent.setControlValue(\attack, "inc");},
				"d"[0], {parent.setControlValue(\attack, "dec");},
				
				"r"[0], {parent.setControlValue(\decay, "inc");},
				"f"[0], {parent.setControlValue(\decay, "dec");}*/
			)
		
		
		});
	
	}
	

}

